package cn.walls1717.ggkt.activity.service.impl;

import cn.walls1717.ggkt.activity.mapper.CouponInfoMapper;
import cn.walls1717.ggkt.activity.service.CouponInfoService;
import cn.walls1717.ggkt.activity.service.CouponUseService;
import cn.walls1717.ggkt.client.user.UserInfoFeignClient;
import cn.walls1717.ggkt.model.activity.CouponInfo;
import cn.walls1717.ggkt.model.activity.CouponUse;
import cn.walls1717.ggkt.model.user.UserInfo;
import cn.walls1717.ggkt.vo.activity.CouponUseQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 优惠券信息服务实现类
 *
 * @author walls1717
 * @since 2022/07/18
 */
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;
    @Autowired
    private CouponUseService couponUseService;

    /**
     * 获取已经使用的优惠券分页列表
     */
    @Override
    public Page<CouponUse> listCouponUsePages(Integer pageNum, Integer pageSize, CouponUseQueryVo couponUseQueryVo) {
        Page<CouponUse> couponUsePage = new Page<>(pageNum, pageSize);
        //获取条件
        Long couponId = couponUseQueryVo.getCouponId();
        String couponStatus = couponUseQueryVo.getCouponStatus();
        String getTimeBegin = couponUseQueryVo.getGetTimeBegin();
        String getTimeEnd = couponUseQueryVo.getGetTimeEnd();
        //封装条件
        LambdaQueryWrapper<CouponUse> lqw = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(couponId)) {
            lqw.eq(CouponUse::getCouponId, couponId);
        }
        if (!StringUtils.isEmpty(couponStatus)) {
            lqw.eq(CouponUse::getCouponStatus, couponStatus);
        }
        // >= getTime
        if (!StringUtils.isEmpty(getTimeBegin)) {
            lqw.ge(CouponUse::getGetTime, getTimeBegin);
        }
        // <= getTime
        if (!StringUtils.isEmpty(getTimeEnd)) {
            lqw.le(CouponUse::getGetTime, getTimeEnd);
        }
        couponUseService.page(couponUsePage, lqw);
        List<CouponUse> records = couponUsePage.getRecords();
        records = records.stream().peek(item->{
            Long userId = item.getUserId();
            if (!StringUtils.isEmpty(userId)) {
                // 远程调用用户模块，根据用户id获取用户信息
                UserInfo userInfo = userInfoFeignClient.innerGetUserById(userId);
                if (userInfo!=null) {
                    item.getParam().put("nickName", userInfo.getNickName());
                    item.getParam().put("phone", userInfo.getPhone());
                }
            }
        }).collect(Collectors.toList());
        couponUsePage.setRecords(records);
        return couponUsePage;
    }

    /**
     * 更新优惠券使用状态
     */
    @Override
    public void updateCouponInfoUseStatus(Long couponUseId, Long orderId) {
        
    }
}
